import Vue from 'vue'
import Router from 'vue-router'
import index from '@/components/Index'
import workinfo from '@/components/WorkInfo'
import BookShelf from '@/components/BookShelf'
import MessageCenter from '@/components/MessageCenter'
import SafeCenter from '@/components/SafeCenter'
import AllClass from '@/components/AllClass'
import writer from '@/components/Writer'
import myWorks from '@/components/MyWorks'
import creation from '@/components/Creation'
import writing from '@/components/Writing'
import ro from "element-ui/src/locale/lang/ro";

Vue.use(Router)
const originalReplace = Router.prototype.replace
Router.prototype.replace = function replace(location) {
    return originalReplace.call(this, location).catch(err => err)
}//解决路由重复报错问题
const router = new Router({
    routes: [
        {
            path: '/',
            name: '/',
            redirect: '/index'
        },
        {
            path: '/index',
            name: 'index',
            component: index,
            meta: {
                title: '首页'
            }
        },
        {
            path: '/BookShelf-left',
            name: 'BookShelf-left',
            component: () => import('../components/BookShelf-left'),
            meta: {
                title: '书架'
            },
            children: [
                {
                    path: '/BookShelf',
                    name: 'BookShelf',
                    component: () => import('../components/BookShelf'),
                    meta: {
                        title: '我的书架'
                    }
                },
                {
                    path: '/SafeCenter',
                    name: 'SafeCenter',
                    component: () => import('../components/SafeCenter'),
                    meta: {
                        title: '安全中心'
                    }
                },
                {
                    path: '/MessageCenter',
                    name: 'MessageCenter',
                    component: () => import('../components/MessageCenter'),
                    meta: {
                        title: '消息中心'
                    }
                },
            ]
        },
        {
            path: '/workinfo',
            name: 'workinfo',
            component: () => import('../components/WorkInfo'),
            meta: {
                title: '作品信息'
            }
        },
        {
            path: '/read',
            name: 'read',
            component: () => import('../components/Read'),
            meta: {
                title: '章节阅读'
            }
        }, {
            path: '/contents',
            name: 'contents',
            component: () => import('../components/contents'),
            meta: {
                title: '目录详情'
            }
        },
        {
            path: '/AllClass',
            name: 'AllClass',
            component: () => import('../components/AllClass'),
            meta: {
                title: '全部分类'
            }
        },
        {
            path: '/writer',
            name: 'writer',
            component: () => import('../components/Writer'),
            meta: {
                title: '作家注册'
            }
        },
        {
            path: '/myWorks',
            name: 'myWorks',
            component: () => import('../components/MyWorks'),
            meta: {
                title: '作品管理'
            }
        },
        {
            path: '/creation',
            name: 'creation',
            component: () => import('../components/Creation'),
            meta: {
                title: '创作管理'
            }
        },
        {
            path: '/writing',
            name: 'writing',
            component: () => import('../components/Writing'),
            meta: {
                title: '写作'
            }
        }
    ],
    mode: 'history'
})
router.beforeEach((to, from, next) => {
    console.log(to.fullPath);
    console.log(from.fullPath)
    document.title = to.meta.title
    if(to.fullPath.startsWith(from.fullPath)){
        //location.reload();
        router
    }
     if (to.fullPath === "/index" || to.fullPath.startsWith("/workinfo") || to.fullPath.startsWith("/read") || to.fullPath.startsWith("/AllClass") || to.fullPath.startsWith("/contents") || sessionStorage.getItem("token")) {
        next();
    } else {
        console.log("未登录");
        next({name: "/"});
    }
})
export default router
